﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Calendar.Components;
using C4F.DevKit.Calendar;

namespace C4F.DevKit.Calendar.Samples
{
    public partial class SelectAppointmentDialog : Form
    {        
        public SelectAppointmentDialog(CalendarService calService)
        {
            InitializeComponent();

            appointmentsList1.CalendarService = calService;
            calendarService1 = calService;
            appointmentsList1.ContextMenuStrip = contextMenuStrip1;

            if (!DesignMode)
            {                
                appointmentsList1.RefreshList();
                Text = string.Format("{0} - {1}",
                    Path.GetFileNameWithoutExtension(calendarService1.CalendarFilePath),
                    Text);
            }
        }

        public void RefreshList()
        {
            appointmentsList1.RefreshList();
        }

        private void btnNewAppt_Click(object sender, EventArgs e)
        {
            AppointmentDialog apptDialog = new AppointmentDialog(calendarService1);
            apptDialog.MdiParent = this.MdiParent;
            apptDialog.Show();
        }

        private void btnEditAppt_Click(object sender, EventArgs e)
        {
            EditSelectedAppointment();
        }

        private void EditSelectedAppointment()
        {
            if (appointmentsList1.SelectedAppointment == null)
            {
                MessageBox.Show("Please select an appointment or create a new appointment");
                return;
            }

            AppointmentDialog apptDialog = new AppointmentDialog(calendarService1,
                appointmentsList1.SelectedAppointment);
            apptDialog.MdiParent = this.MdiParent;
            apptDialog.Show();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                calendarService1.ImportCalendar();
                appointmentsList1.RefreshList();
            }
            catch (Exception)
            {
                MessageBox.Show(
                    String.Format("Could not open calendar {0}. Please choose a different file", calendarService1.CalendarFilePath),
                    "Error while trying to open calendar");
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            string exportTarget = calendarService1.PromptForSaveLocation();

            if (string.IsNullOrEmpty(exportTarget))
            {
                return;
            }

            try
            {
                calendarService1.ExportCalendar(exportTarget);
            }
            catch (Exception)
            {
                MessageBox.Show(
                    String.Format("Could not write to file {0}. Please choose a different file to save to.", calendarService1.CalendarFilePath),
                    "Error while exporting calendar");
            }
        }

        private void exportAppointmentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = calendarService1.PromptForOpenLocation();
            
            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }

            calendarService1.Export(appointmentsList1.SelectedAppointment, filePath);
        }

        private void importAppointmentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string newCalFilePath = calendarService1.PromptForOpenLocation();

            if (string.IsNullOrEmpty(newCalFilePath))
            {
                return;
            }

            try
            {
                ImportAppointmentDialog importDialog = new ImportAppointmentDialog(this, newCalFilePath);
                importDialog.MdiParent = this.MdiParent;
                importDialog.Show();
            }
            catch (Exception)
            {
                MessageBox.Show(
                    String.Format("Error while trying to load calendar {0}. Please select another calendar file"),
                    "Error while loading calendar");
            }
        }

        public void Import(Appointment appt)
        {
            calendarService1.Import(appt, calendarService1.CalendarFilePath);
            appointmentsList1.RefreshList();
        }
    }
}
